import torch

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv("D:\learn\深度学习\day53_pytorch入门(二)\代码\dataset\Income1.csv")
educationArray = np.array(data.Education.values)
incomeArray = np.array(data.Income.values)

X = torch.from_numpy(educationArray.reshape(-1, 1)).type(torch.float32)
Y = torch.from_numpy(incomeArray.reshape(-1, 1)).type(torch.float32)


model = torch.nn.Linear(1, 1)
loss_fn = torch.nn.MSELoss()
opt = torch.optim.SGD(model.parameters(), lr=0.001)

for epoch in range(5000):
    for x, y in zip(X, Y):
        y_pred = model(x)
        loss = loss_fn(y, y_pred)
        # 梯度清零
        opt.zero_grad()
        loss.backward()
        # 更新
        opt.step()

print(model.weight)
print(model.bias)
